package com.tao.action.group;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.google.gson.Gson;
import com.gxuwz.easyui.model.tree.function.FunAttributes;
import com.gxuwz.easyui.model.tree.function.FunChildrenTree;
import com.gxuwz.easyui.model.tree.function.FunGrandChildrenTree;
import com.gxuwz.easyui.model.tree.function.FunParentTree;
import com.gxuwz.easyui.model.tree.wx.ChildrenTree;
import com.gxuwz.easyui.model.tree.wx.GrandChildrenTree;
import com.gxuwz.easyui.model.tree.wx.GrandGrandChildrenTree;
import com.gxuwz.easyui.model.tree.wx.GroupGrandGrandChildrenTree;
import com.gxuwz.easyui.model.tree.wx.ParentTree;
import com.opensymphony.xwork2.interceptor.annotations.Allowed;
import com.tao.action.BaseAction;
import com.tao.domain.Group;
import com.tao.domain.User;
import com.tao.service.group.GroupService;
import com.tao.service.user.UserService;

@SuppressWarnings("serial")
public class GroupAction extends BaseAction implements RequestAware,SessionAware{
 
	
	private Map request;
	private Map session;
	private Group group;
	@Allowed
	UserService userService;
	@Allowed
	GroupService groupService;
	
	
	List<User> userlist;
	List<Group> grouplist;

	@Override
	public String execute() throws Exception {
		
		userlist = userService.findAll(null);
		getResult(userlist);
		
		return SUCCESS;
	}
  public void getGroupList() {
		
		userlist = userService.findAll(null);
		getResult(userlist);
		
		
	}
	
	public void getResult(List<User> userlist){
		 HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			try {
					response.getWriter().print(new Gson().toJson(userlist));
					
			} catch (IOException e) {
			}
	}
	public void tree(){
		userlist =  userService.findAll(null);
		grouplist =  groupService.findAll(null);
				
		List<GrandGrandChildrenTree> ggList = new ArrayList<GrandGrandChildrenTree>();
		List<GrandChildrenTree> gList = new ArrayList<GrandChildrenTree>();
		List<ChildrenTree> cList = new ArrayList<ChildrenTree>();
		
		for(User user:userlist){
			List<GroupGrandGrandChildrenTree> gggList = new ArrayList<GroupGrandGrandChildrenTree>();
		for(Group group:grouplist){
			if(group.getUin().equals(user.getUin())){
		GroupGrandGrandChildrenTree groupGrandGrandChildrenTree = new GroupGrandGrandChildrenTree();
		groupGrandGrandChildrenTree.setText(group.getGroupNickName());
		groupGrandGrandChildrenTree.setUsername(user.getUserName());
		groupGrandGrandChildrenTree.setGroupname(group.getGroupName());
		gggList.add(groupGrandGrandChildrenTree);
		 }
		}
		
		GrandGrandChildrenTree grandgrandChildrenTree = new GrandGrandChildrenTree();
		grandgrandChildrenTree.setText(user.getNickName());
		grandgrandChildrenTree.setId(4);
		grandgrandChildrenTree.setChildren(gggList);
		ggList.add(grandgrandChildrenTree);
		
		}
		
		GrandChildrenTree grandChildrenTree = new GrandChildrenTree();
		grandChildrenTree.setId(3);
		grandChildrenTree.setText("富民派出所");
		grandChildrenTree.setChildren(ggList);
		gList.add(grandChildrenTree);
		
		
		ChildrenTree childrenTree = new ChildrenTree();
		childrenTree.setId(2);
		childrenTree.setText("万秀分局");
		childrenTree.setChildren(gList);
		cList.add(childrenTree);		
		
		
		ParentTree parentTree = new ParentTree();
		parentTree.setId(1);
		parentTree.setText("梧州市公安局");
		parentTree.setChildren(cList);
		
		String treeJson ="["+ new Gson().toJson(parentTree)+"]";
		
		
		getTreeResult(treeJson);
	}
	//左侧菜单生成
	public void funTree() {
		List<FunChildrenTree> fctl = new ArrayList<FunChildrenTree>();
		FunParentTree fpt = new FunParentTree();
		
		
		List<FunGrandChildrenTree> fgctfun0 = new ArrayList<FunGrandChildrenTree>();

		FunAttributes attributes0 = new FunAttributes();
		attributes0.setUrl("admin/user_getHttpUserList.action");
		
		FunGrandChildrenTree fgct0 = new FunGrandChildrenTree();
		fgct0.setText("扫码登录");
		fgct0.setAttributes(attributes0);
		
		fgctfun0.add(fgct0);
		
		FunChildrenTree fct = new FunChildrenTree();
		fct.setId(2);
		fct.setText("微信管理");
		fct.setChildren(fgctfun0);
		/***********************************************/
		List<FunGrandChildrenTree> fgctfun1 = new ArrayList<FunGrandChildrenTree>();
		
		FunAttributes attributes1 = new FunAttributes();
		attributes1.setUrl("send_msg.jsp");
		
		FunGrandChildrenTree fgct1 = new FunGrandChildrenTree();
		fgct1.setText("发送信息");
		fgct1.setAttributes(attributes1);
		fgctfun1.add(fgct1);
		FunChildrenTree fct1 = new FunChildrenTree();
		fct1.setId(2);
		fct1.setText("发送管理");
		fct1.setChildren(fgctfun1);
		/***********************************************/
		
		List<FunGrandChildrenTree> fgctfun2 = new ArrayList<FunGrandChildrenTree>();

		FunAttributes attributes2 = new FunAttributes();
		attributes2.setUrl("wx_list.jsp");
		
		FunGrandChildrenTree fgct2 = new FunGrandChildrenTree();
		fgct2.setText("微信状态监控");
		fgct2.setAttributes(attributes2);
		
		fgctfun2.add(fgct2);
		FunChildrenTree fct2 = new FunChildrenTree();
		fct2.setId(2);
		fct2.setText("监控管理");
		fct2.setChildren(fgctfun2);
		
		fctl.add(fct);
		fctl.add(fct1);
		fctl.add(fct2);
		
		fpt.setId(1);
		fpt.setText("大巡防");
		fpt.setChildren(fctl);
		
		
		
		
		String treeJson ="["+ new Gson().toJson(fpt)+"]";
		
		
		getTreeResult(treeJson);
		
	}
	public void getTreeResult(String dataTree){

		 HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			
			try {
				response.getWriter().print(dataTree);
			} catch (IOException e) {
			}
	}
	
	 public String Login(){
		 return SUCCESS;
		
		 }

	public Group getGroup() {
		
		
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Map getRequest() {
		return request;
	}

	public void setRequest(Map request) {
		this.request = request;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public List<User> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<User> userlist) {
		this.userlist = userlist;
	}

	public List<Group> getGrouplist() {
		return grouplist;
	}

	public void setGrouplist(List<Group> grouplist) {
		this.grouplist = grouplist;
	}
	
}
